﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MiniBottles.Models;
using System.IO;
using System.Drawing;

namespace MiniBottles.Controllers
{
    [Authorize]
    public class DictCollectionController : Controller
    {
        private DataContext db = new DataContext();

        //
        // GET: /DictCollection/

        public ActionResult Index()
        {
            return View(db.DictCollection.Where(x=> !x.IsDeleted).ToList());
        }

        //
        // GET: /DictCollection/Create

        public ActionResult Create()
        {
            ViewBag.ImageRequied = true;
            DictCollection dictcollection = new DictCollection();
            return View(dictcollection);
        }

        //
        // POST: /DictCollection/Create

        [HttpPost]
        public ActionResult Create(DictCollection dictcollection, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                var path = Path.Combine(Server.MapPath("~/Upload/"));
                dictcollection.FileName = this.SaveFiles(file,path);

                db.DictCollection.Add(dictcollection);
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            return View(dictcollection);
        }

        //
        // GET: /DictCollection/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ViewBag.ImageRequied = false;
            DictCollection dictcollection = db.DictCollection.Find(id);
            if (dictcollection == null)
            {
                return HttpNotFound();
            }
            return View(dictcollection);
        }

        //
        // POST: /DictCollection/Edit/5

        [HttpPost]
        public ActionResult Edit(DictCollection dictcollection, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                DictCollection dictcollectionDb = db.DictCollection.Find(dictcollection.Id);

                dictcollectionDb.Name = dictcollection.Name;
                dictcollectionDb.IsActive = dictcollection.IsActive;

                var path = Path.Combine(Server.MapPath("~/Upload/"));
                if (file != null)
                {
                    dictcollectionDb.FileName = this.SaveFiles(file, path);
                }

                db.Entry(dictcollectionDb).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(dictcollection);
        }

        private string SaveFiles(HttpPostedFileBase file, string path)
        {
            var fileName = string.Empty;
            if (file != null)
            {
                var guid = Guid.NewGuid();
                fileName = guid + "_" + Path.GetFileName(file.FileName);

                Image img = Image.FromStream(file.InputStream, true, true);

                Image bigImg = Helper.ScaleImage(img, widthSize: 700);
                var pathbig = Path.Combine(path + "big", fileName);
                bigImg.Save(pathbig);


                Image img2 = Image.FromStream(file.InputStream, true, true);

                Image thumb = Helper.ScaleImage(img2, maxSize: 150);
                var pathmini = Path.Combine(path + "mini", fileName);
                thumb.Save(pathmini);
            }

            return fileName;
        }
        //
        // GET: /DictCollection/Delete/5

        public ActionResult Delete(int id = 0)
        {
            DictCollection dictcollection = db.DictCollection.Find(id);
            if (dictcollection == null)
            {
                return HttpNotFound();
            }
            return View(dictcollection);
        }

        //
        // POST: /DictCollection/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            DictCollection dictcollection = db.DictCollection.Find(id);

            if(dictcollection.Categories.Where(x=> !x.IsDeleted).Count() < 1)
            {
                dictcollection.IsDeleted = true;
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}